package cn.itsource.controller;

import cn.itsource.domain.Article;
import cn.itsource.dto.ArticleDat;
import cn.itsource.dto.ArticleDto;
import cn.itsource.query.Query;
import cn.itsource.service.ArticleService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //注意前端地址：http://localhost:8080/article - get
    @GetMapping
    public List<Article> findAll() {
        return articleService.findAll();
    }


    //注意前端地址：http://localhost:8080/article/1 - get
    @GetMapping("/{id}")
    public Article findOne(@PathVariable("id") Long id) {
        return articleService.findOne(id);
    }


    //注意前端地址：http://localhost:8080/article - put - {}

    @PutMapping
    public AjaxResult addorUpdate(@RequestBody Article article) {
        try {
            if (article.getId() == null) {//添加操作
                articleService.add(article);
            } else {
                articleService.update(article);
            }
            //如果成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    //注意前端地址：http://localhost:8080/article/1 - delete

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            articleService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    //分页+模糊查询
    @PostMapping
    public PageInfo<Article> pageQuery(@RequestBody Query query) {

        //前端接收参数：total，list
        return articleService.pageQuery(query);
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchdel(@RequestBody Long[] ids) {
        try {
            articleService.patchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    //获取文章类型
    @GetMapping("/articleType")
    public List<ArticleDat> articleType() {
        return articleService.articleType();
    }


    //获取时间统计
    @GetMapping("/timeCount")
    public List<ArticleDat> timecount() {
        return articleService.timecount();
    }

    //获取相关文章内容
    @GetMapping("/relatedArticles/{id}")
    public List<Article> relatedArticles(@PathVariable("id") Long id) {
        return articleService.relatedArticles(id);
    }


    //根据article_typeid 获取文章数据
    @GetMapping("/articleList/{articleTypeId}")
    public List<Article> articleList(@PathVariable("articleTypeId") Long articleTypeId) {
        return articleService.articleList(articleTypeId);
    }



    //树状图
    @GetMapping("/getNameandReadCount")
    public List<Article> getNameandReadCount() {
        return articleService.getNameandReadCount();
    }

    //扇形
    @GetMapping("/sector")
    public List<ArticleDat> sector() {
        return articleService.sector();
    }


    //获取按照年份分组
    @GetMapping("/yearGrougBy")
    public List<Article> yearGrougBy() {
        return articleService.yearGrougBy();
    }

    //归档：
    @GetMapping("/getBlogGdList")
    public List<ArticleDto> getBlogGdList(){
        return articleService.getBlogGdList();
    }


    /**
     * 前台系统点赞功能接口
     * @param id
     * @param request
     * @return
     */
    @PutMapping("/updateStar/{id}")
    @ApiOperation(value = "前台系统 - 点赞功能")
    public AjaxResult updateStar(@PathVariable("id") Long id, HttpServletRequest request) {
        try {
            return articleService.updateStar(id,request);
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return new AjaxResult(false, "网络异常，请稍后再试！！！");
        }

    }


    /**
     * 业务接口：前台点赞图片样式加载
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/initIconClass/{id}")
    @ApiOperation(value = "前台系统 - 页面加载点赞图片样式")
    public AjaxResult initIconClass(@PathVariable("id")Long id,HttpServletRequest request){
        try {
            return articleService.initIconClass(id,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常，请稍后再试！！！");
        }
    }

}
